package com.yy.QSManage.api.other;

import com.yy.QSManage.common.Result;
import com.yy.QSManage.service.other.scheduler.ITaskSchedulerService;
import com.yy.QSManage.service.other.scheduler.JobQuery;
import com.yy.QSManage.service.other.scheduler.TaskScheduler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@Api( tags = "定时任务")
@RequestMapping("/TaskSchedulerApi")
public class TaskSchedulerApi {

    @Resource
    private ITaskSchedulerService schedulerService;

    /**
     * 添加定时任务信息
     *
     * @param taskScheduler 定时任务信息
     * @return ReturnModel 添加定时任务
     */
    @PostMapping(value = "save")
    @ApiImplicitParam(name = "query", value = "查询参数", required = true, dataType = "TaskScheduler", paramType = "body")
    public Result<Boolean> save(@RequestBody TaskScheduler taskScheduler) {
//        {
//            "jobName":"bookTask2",
//                "description":"书籍定时任务",
//                "jobTypeRadio":"expression",
//                "startTime":"2024-01-12 15:20:00",
//                "endTime":"2024-01-13 00:00:00",
//                "jobClass":"com.example.demo.system.controller.BookTask",
//                "cronExpression":"*/30 * * * * ?"
//        }
        schedulerService.save(taskScheduler);
        return Result.success();
    }

    /**
     * 移除定时任务
     *
     * @param taskScheduler 定时任务信息
     * @return ReturnModel 移除定时任务
     */
    @PostMapping(value = "/delete")
    public Result<String> delete(@RequestBody TaskScheduler taskScheduler) {
//        {
//            "jobName": "bookTask"
//        }
        schedulerService.delete(taskScheduler.getJobName());
        return Result.success(taskScheduler.getJobName());
    }

    /**
     * 修改定时任务
     *
     * @param taskScheduler 定时任务信息
     * @return ReturnModel 修改定时任务
     */
    @PostMapping(value = "update")
    public Result<String> update(@RequestBody TaskScheduler taskScheduler) {
//        {
//            "jobName":"bookTask",
//                "description":"1",
//                "jobTypeRadio":"expression",
//                "startTime":"2024-01-13 14:00:00",
//                "endTime":"",
//                "jobClass":"com.example.demo.system.controller.BookTask",
//                "cronExpression":"*/30 * * * * ?"
//        }
        schedulerService.update(taskScheduler);
        return Result.success(taskScheduler.getJobName());
    }

    /**
     * 暂停定时任务
     *
     * @param taskScheduler 定时任务名称
     * @return ReturnModel 暂停定时任务
     */
    @PostMapping(value = "pause")
    public Result<String> pause(@RequestBody TaskScheduler taskScheduler) {
//        {
//            "jobName": "bookTask2"
//        }
        schedulerService.pause(taskScheduler.getJobName());
        return Result.success(taskScheduler.getJobName());
    }

    /**
     * 恢复定时任务
     *
     * @param taskScheduler 定时任务名称
     * @return ReturnModel 恢复定时任务
     */
    @PostMapping(value = "resume")
    public Result<String> resume(@RequestBody TaskScheduler taskScheduler) {
//        {
//            "jobName": "bookTask2"
//        }
        schedulerService.resume(taskScheduler.getJobName());
        return Result.success(taskScheduler.getJobName());
    }

    /**
     * 执行定时任务
     *
     * @param taskScheduler 定时任务名称
     * @return ReturnModel 执行定时任务
     */
    @PostMapping(value = "executeJob")
    public Result<String> executeJob(@RequestBody TaskScheduler taskScheduler) {
//        {
//            "jobName": "bookTask2"
//        }
        schedulerService.executeJob(taskScheduler.getJobName());
        return Result.success(taskScheduler.getJobName());
    }

    /**
     * 查询单个定时任务信息
     *
     * @param jobName 任务名称
     * @return ReturnModel 查询单个定时任务信息
     */
    @GetMapping(value = "selectByName")
    public Result<TaskScheduler> selectByName(@RequestParam("jobName") String jobName) {
        TaskScheduler taskScheduler = schedulerService.selectByName(jobName);
        return Result.success(taskScheduler);
    }

    /**
     * 查询定时任务列表
     *
     * @param jobQuery 查询条件
     * @return ReturnModel 查询定时任务列表
     */
    @PostMapping(value = "selectList")
    public Result<List<TaskScheduler>> selectList(@RequestBody JobQuery jobQuery) {
//        {
//            "jobNameLike": ""
//        }
        List<TaskScheduler> taskSchedulers = schedulerService.selectList(jobQuery);
        return Result.success(taskSchedulers);
    }
}